/**
 * 
 */
package com.adkfp.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import net.sf.json.JSONObject;

/**
 * @author qwertyuiop
 *
 */
public class ControllerTime {

private Connection conn ;
    
    private String dbdriver ;
    private String dbhost;
    private String dbuser;
    private String dbpassword;
    
    public ControllerTime() throws IOException, ClassNotFoundException, SQLException  {
    	Properties properties = new Properties();
    	// 使用ClassLoader加载properties配置文件生成对应的输入流
    	//InputStream in = Reader.class.getResourceAsStream("/database.properties"); 
    	InputStream in = Reader.class.getResourceAsStream("/database.properties");
    	// 使用properties对象加载输入流
        properties.load(in);
    	//获取key对应的value值
		
    	Class.forName(properties.getProperty("jdbc.driverClass"));//"com.mysql.jdbc.Driver");
		dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		dbuser = properties.getProperty("jdbc.user","root");
		dbpassword = properties.getProperty("jdbc.password", "123456");
		conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }  

    
	public ControllerTime( String dbdriver, String dbhost, String dbuser, String dbpassword) throws Exception {
		super();
		this.dbdriver = dbdriver;
		this.dbhost = dbhost;
		this.dbuser = dbuser;
		this.dbpassword = dbpassword;
		Class.forName(this.dbdriver);//"com.mysql.jdbc.Driver");
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
	}
    
	
	public JSONObject getWeekHoliday(String id) throws Exception {
		
		Statement state = conn.createStatement();
		String SQLStr = " select WeekHoliday from ADK_controller where AutoID="+id ;
		ResultSet rs = state.executeQuery(SQLStr);
		//JSONObject jsonObject = new JSONObject();
		//jsonObject.put(key, value)
		rs.next();
		return unpack(rs.getString("WeekHoliday"));
		
	}
	//合
	public static String pack(JSONObject jsobj) {
		System.err.println("----+++------"+jsobj);
		String s ;
		s = "00" + jsobj.getString("starttime1")+jsobj.getString("endtime1");
		s = s + "01" + jsobj.getString("starttime2")+jsobj.getString("endtime2");
		s = s + "02" + jsobj.getString("starttime3")+jsobj.getString("endtime3");
		s = s + "03" + jsobj.getString("starttime4")+jsobj.getString("endtime4");
		s = s + "04" + jsobj.getString("starttime5")+jsobj.getString("endtime5");
		s = s + "05" + jsobj.getString("starttime6")+jsobj.getString("endtime6");
		s = s + "06" + jsobj.getString("starttime7")+jsobj.getString("endtime7");
		return s ;
	}
	
	//拆
	public static JSONObject unpack(String weekholiday) {
	   String starttime1=weekholiday.substring(2,4)+":"+weekholiday.substring(4, 6);
	   String endtime1=weekholiday.substring(6,8)+":"+weekholiday.substring(8,10);
	   
	   String starttime2=weekholiday.substring(12, 14)+":"+weekholiday.substring(14,16);
	   String endtime2=weekholiday.substring(16,18)+":"+weekholiday.substring(18,20);
	   
	   String starttime3=weekholiday.substring(22, 24)+":"+weekholiday.substring(24,26);
	   String endtime3=weekholiday.substring(26,28)+":"+weekholiday.substring(28,30);
	   
	   String starttime4=weekholiday.substring(32, 34)+":"+weekholiday.substring(34,36);
	   String endtime4=weekholiday.substring(36,38)+":"+weekholiday.substring(38,40);
	   
	   String starttime5=weekholiday.substring(42, 44)+":"+weekholiday.substring(44,46);
	   String endtime5=weekholiday.substring(46,48)+":"+weekholiday.substring(48,50);
	   
	   String starttime6=weekholiday.substring(52, 54)+":"+weekholiday.substring(54,56);
	   String endtime6=weekholiday.substring(56,58)+":"+weekholiday.substring(58,60);
	   
	   String starttime7=weekholiday.substring(62, 64)+":"+weekholiday.substring(64,66);
	   String endtime7=weekholiday.substring(66,68)+":"+weekholiday.substring(68,70);
	   JSONObject jsonObject = new JSONObject();
	   //星期日
	   jsonObject.put("starttime1", starttime1);
	   jsonObject.put("endtime1", endtime1);
	   //星期一
	   jsonObject.put("starttime2", starttime2);
	   jsonObject.put("endtime2", endtime2);
	   //星期二
	   jsonObject.put("starttime3", starttime3);
	   jsonObject.put("endtime3", endtime3);
	   //星期三
	   jsonObject.put("starttime4", starttime4);
	   jsonObject.put("endtime4", endtime4);
	  //星期四
	   jsonObject.put("starttime5", starttime5);
	   jsonObject.put("endtime5", endtime5);
	  //星期五
	   jsonObject.put("starttime6", starttime6);
	   jsonObject.put("endtime6", endtime6);
	  //星期六
	   jsonObject.put("starttime7", starttime7);
	   jsonObject.put("endtime7", endtime7);
		return jsonObject;
	   
   }
	
	
	public JSONObject getNONCParam(String id) throws Exception {
		Statement state = conn.createStatement();
		String SQLStr = " select NONCParam from ADK_Door where AutoID="+id ;
		ResultSet rs = state.executeQuery(SQLStr);
		//JSONObject jsonObject = new JSONObject();
		//jsonObject.put(key, value)
		rs.next();
		return longOpen(rs.getString("NONCParam"));
		
	}
	
	//拆常开/常闭
	public static JSONObject longOpen(String noncparam) {
		//常开 AA-禁止 55-使能
		String opentime=noncparam.substring(0,2);
		// 第一常开时段开始时分
		String startopen1=noncparam.substring(2,4)+":"+noncparam.substring(4,6);
		// 第一常开时段结束时分
		String endopen1=noncparam.substring(6,8)+":"+noncparam.substring(8,10);
		// 第二常开时段开始时分
		String startopen2=noncparam.substring(10,12)+":"+noncparam.substring(12,14);
		// 第二常开时段开始时分
	    String endopen2=noncparam.substring(14,16)+":"+noncparam.substring(16,18);
	    // 第三常开时段开始时分
	    String startopen3=noncparam.substring(18,20)+":"+noncparam.substring(20,22);
	    // 第三常开时段开始时分
	    String endopen3=noncparam.substring(22,24)+":"+noncparam.substring(24,26);
	    //常开时段休息日
	    String openweek=noncparam.substring(26,30);
	    
	    
	       //常闭 AA-禁止 55-使能
	  		String  closetime=noncparam.substring(30,32);
	  		// 第一常闭时段开始时分
	  		String startclose1=noncparam.substring(32,34)+":"+noncparam.substring(34,36);
	  		// 第一常闭时段结束时分
	  		String endclose1=noncparam.substring(36,38)+":"+noncparam.substring(38,40);
	  		// 第二常闭时段开始时分
	  		String startclose2=noncparam.substring(40,42)+":"+noncparam.substring(42,44);
	  		// 第二常闭时段开始时分
	  	    String endclose2=noncparam.substring(44,46)+":"+noncparam.substring(46,48);
	  	    // 第三常闭时段开始时分
	  	    String startclose3=noncparam.substring(48,50)+":"+noncparam.substring(50,52);
	  	    // 第三常闭时段开始时分
	  	    String endclose3=noncparam.substring(52,54)+":"+noncparam.substring(54,56);
	  	    //常闭时段休息日
		    String closeweek=noncparam.substring(56,60);
	  	    
	  	  JSONObject jsonObject = new JSONObject();
	  	  jsonObject.put("opentime", opentime);
	  	  jsonObject.put("startopen1", startopen1);
	  	  jsonObject.put("endopen1", endopen1);
	  	  jsonObject.put("startopen2", startopen2);
	  	  jsonObject.put("endopen2", endopen2);
	  	  jsonObject.put("startopen3", startopen3);
	  	  jsonObject.put("endopen3", endopen3);
	  	  jsonObject.put("openweek", openweek);
	  	  jsonObject.put("closetime", closetime);
	  	  jsonObject.put("startclose1", startclose1);
	  	  jsonObject.put("endclose1", endclose1);
	  	  jsonObject.put("startclose2", startclose2);
	  	  jsonObject.put("endclose2", endclose2);
	  	  jsonObject.put("startclose3", startclose3);
	  	  jsonObject.put("endclose3", endclose3);
	  	  jsonObject.put("closeweek", closeweek);
	  	  //调用常开（拆）
	  	  DataTimepack(jsonObject,openweek);
	  	  //System.out.println("常开的休息日是："+openweek);
	  	dataTimeB(jsonObject,closeweek);
		return jsonObject;
		
	}
	
	//合 常开/常闭
	public static String longOpenpack(JSONObject jsobj) {
		String s ="";
		s = s + jsobj.getString("opentime");
		s = s + jsobj.getString("startopen1")+jsobj.getString("endopen1");
		s = s + jsobj.getString("startopen2")+jsobj.getString("endopen2");
		s = s + jsobj.getString("startopen3")+jsobj.getString("endopen3");
		s = s + jsobj.getString("weektime")+jsobj.getString("closetime");
		s = s + jsobj.getString("startclose1")+jsobj.getString("endclose1");
		s = s + jsobj.getString("startclose2")+jsobj.getString("endclose2");
		s = s + jsobj.getString("startclose3")+jsobj.getString("endclose3");
		s = s +  jsobj.getString("weektimeB");
		//System.err.println("-----====+++===----"+s);
		return s ;
	}
	
	
	public JSONObject getNoWarnTime(String id) throws Exception {
		Statement state = conn.createStatement();
		String SQLStr = " select NoWarnTime from ADK_Door where AutoID="+id ;
		ResultSet rs = state.executeQuery(SQLStr);
		//JSONObject jsonObject = new JSONObject();
		//jsonObject.put(key, value)
		rs.next();
		return OpenPolice(rs.getString("NoWarnTime"));
		
	}

   //拆(报警设防)
	public static JSONObject OpenPolice(String nowarntime) {
		//是否使能非报警功能 AA-禁止 55-使能
		String otimep=nowarntime.substring(0,2);
		// 第一时段开始时分
		String stimep1=nowarntime.substring(2,4)+":"+nowarntime.substring(4,6);
		//第一段结束时分
		String etimep1=nowarntime.substring(6,8)+":"+nowarntime.substring(8,10);
		//第二时段开始时分
		String stimep2=nowarntime.substring(10,12)+":"+nowarntime.substring(12,14);
		//第二段结束时分
		String etimep2=nowarntime.substring(14,16)+":"+nowarntime.substring(16,18);
		// 第一时段开始时分
		String stimep3=nowarntime.substring(18,20)+":"+nowarntime.substring(20,22);
	    //第一段结束时分
		String etimep3=nowarntime.substring(22,24)+":"+nowarntime.substring(24,26);
		//休息日
		String wtimep=nowarntime.substring(26,30);
		
		System.out.println("数据库中的wtimep是："+wtimep);
		
		 JSONObject jsonObject = new JSONObject();
		 jsonObject.put("otimep", otimep);
		 jsonObject.put("stimep1", stimep1);
		 jsonObject.put("etimep1", etimep1);
		 jsonObject.put("stimep2", stimep2);
		 jsonObject.put("etimep2", etimep2);
		 jsonObject.put("stimep3", stimep3);
		 jsonObject.put("etimep3", etimep3);
		 jsonObject.put("wtimep", wtimep);
		 //报警（拆）
		 dataTimePo(jsonObject,wtimep);
		 
		return jsonObject;
	}
    
	//合 (报警设防)
		public static String OpenPolicepack(JSONObject jsobj) {
			System.err.println("哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
			String s ;
			s=jsobj.getString("otimep");
			s = s + jsobj.getString("stimep1")+jsobj.getString("etimep1");
			s = s + jsobj.getString("stimep2")+jsobj.getString("etimep2");
			s = s + jsobj.getString("stimep3")+jsobj.getString("etimep3");
			s = s +jsobj.getString("wtimep");
			return s ;
		}
		
		//合 常开休息日
		public static String DataTimeunpack(JSONObject jsobj) {
			String s="" ;
			//System.err.println("------====---------"+jsobj.getString("sun"));
			Integer sunday=Integer.parseInt(jsobj.getString("sun"));
			Integer monday=Integer.parseInt(jsobj.getString("mon"));
			Integer tuesday=Integer.parseInt(jsobj.getString("tues"));
			Integer wednesday=Integer.parseInt(jsobj.getString("wed"));
			Integer thursday=Integer.parseInt(jsobj.getString("thur"));
			Integer friday=Integer.parseInt(jsobj.getString("fri"));
			Integer saturday=Integer.parseInt(jsobj.getString("sat"));
		Integer i =  (1<<sunday)+(4<<monday)
					         +(16<<tuesday)+(64<<wednesday)
					         +(256<<thursday)+(1024<<friday)
					         +(4096<<saturday);
		
/*		System.out.println("i是-----------"+i);
		Integer sunn=1<<sunday;
		System.out.println("Sunday是---------"+sunn);
		Integer monn=4<<monday;
        System.out.println("Monday是---------"+monn);
        Integer tuesn=16<<tuesday;
        System.out.println("tuesday是---------"+tuesn);
        Integer wedn=64<<wednesday;
        System.out.println("wednesday是---------"+wedn);
        Integer thurn=256<<thursday;
        System.out.println("thursday是---------"+thurn);
        Integer frin=1024<<friday;
        System.out.println("friday是---------"+frin);
        Integer satn=4096<<saturday;
        System.out.println("saturday是---------"+satn);*/
        
		s=Integer.toHexString(i);//.substring(4, 8);
		return s ;
		}
		
		//常开时段休息日    拆（放入前台）
		public static void DataTimepack(JSONObject obj ,String openweek) {
			
			Integer inttime=Integer.valueOf(openweek,16);
			//星期日
			System.out.println("inttime是：===="+inttime);
			byte sun = (byte) (0x0002 & inttime) ;// 整日、非整日
			obj.put("sun",sun);
			System.out.println("数据库中的Sunday是：==="+sun);
			//星期一
			byte mon= (byte) (((0x0002<<2) & inttime)>>2);// 整日、非整日
			obj.put("mon", mon);
			//星期二
			byte tues=(byte) (((0x0002<<4) & inttime)>>4);// 整日、非整日
			obj.put("tues", tues);
			//星期三
			byte wed=(byte) (((0x0002<<6) & inttime)>>6);// 整日、非整日
			obj.put("wed", wed);
			//星期四
			byte thur= (byte) (((0x0002<<8) & inttime)>>8);// 整日、非整日
			obj.put("thur", thur);
			//星期五
			byte fri= (byte) (((0x0002<<10) & inttime)>>10);// 整日、非整日
			obj.put("fri", fri);
			//星期六
			byte sat= (byte) (((0x0002<<12) & inttime)>>12);// 整日、非整日
			obj.put("sat", sat);
/*			System.out.println("数据库中的星期一是：----"+mon);
			System.out.println("数据库中的星期二是：----"+tues);
			System.out.println("数据库中的星期三是：----"+wed);
			System.out.println("数据库中的星期四是：----"+thur);
			System.out.println("数据库中的星期五是：----"+fri);
			System.out.println("数据库中的星期六是：----"+sat);*/
			
		}
		
		
		//常闭休息日（拆）
		public static void dataTimeB(JSONObject obj ,String closeweek) {
			Integer inttime1=Integer.valueOf(closeweek,16);
			//星期日
			byte unsun = (byte) (0x0002 & inttime1) ;// 整日、非整日
			obj.put("unsun", unsun);
			//星期一
			byte unmon= (byte) (((0x0002<<2) & inttime1)>>2);// 整日、非整日
			obj.put("unmon", unmon);
			//星期二
			byte untues=(byte) (((0x0002<<4) & inttime1)>>4);// 整日、非整日
			obj.put("untues", untues);
			//星期三
			byte unwed=(byte) (((0x0002<<6) & inttime1)>>6);// 整日、非整日
			obj.put("unwed", unwed);
			//星期四
			byte unthur= (byte) (((0x0002<<8) & inttime1)>>8);// 整日、非整日
			obj.put("unthur", unthur);
			//星期五
			byte unfri= (byte) (((0x0002<<10) & inttime1)>>10);// 整日、非整日
			obj.put("unfri", unfri);
			//星期六
			byte unsat= (byte) (((0x0002<<12) & inttime1)>>12);// 整日、非整日
			obj.put("unsat", unsat);
		}
		
		
		//合 常开休息日
				public static String DataTimeunpackB(JSONObject jsobj) {
					String s="" ;
					Integer sunday=Integer.parseInt(jsobj.getString("unsun"));
					Integer monday=Integer.parseInt(jsobj.getString("unmon"));
					Integer tuesday=Integer.parseInt(jsobj.getString("untues"));
					Integer wednesday=Integer.parseInt(jsobj.getString("unwed"));
					Integer thursday=Integer.parseInt(jsobj.getString("unthur"));
					Integer friday=Integer.parseInt(jsobj.getString("unfri"));
					Integer saturday=Integer.parseInt(jsobj.getString("unsat"));
				Integer i =  (1<<sunday)+(4<<monday)
							         +(16<<tuesday)+(64<<wednesday)
							         +(256<<thursday)+(1024<<friday)
							         +(4096<<saturday);	        
				s=Integer.toHexString(i);//.substring(4, 8);
				return s ;
				}
				
				
				//报警休息日（拆）
				public static void dataTimePo(JSONObject obj ,String wtimep) {
					System.out.println("方法传入的wtimep是"+wtimep);
					Integer inttime2=Integer.valueOf(wtimep,16);
					System.out.println("报警数据库传来的inttime2是====="+inttime2);
					//星期日
					byte sun1 = (byte) (0x0002 & inttime2) ;// 整日、非整日
					obj.put("sun1", sun1);
					//星期一
					byte mon1= (byte) (((0x0002<<2) & inttime2)>>2);// 整日、非整日
					obj.put("mon1", mon1);
					//星期二
					byte tues1=(byte) (((0x0002<<4) & inttime2)>>4);// 整日、非整日
					obj.put("tues1", tues1);
					//星期三
					byte wed1=(byte) (((0x0002<<6) & inttime2)>>6);// 整日、非整日
					obj.put("wed1", wed1);
					//星期四
					byte thur1= (byte) (((0x0002<<8) & inttime2)>>8);// 整日、非整日
					obj.put("thur1", thur1);
					//星期五
					byte fri1= (byte) (((0x0002<<10) & inttime2)>>10);// 整日、非整日
					obj.put("fri1", fri1);
					//星期六
					byte sat1= (byte) (((0x0002<<12) & inttime2)>>12);// 整日、非整日
					obj.put("sat1", sat1);
				}	
				
				
				//合 报警休息日
				public static String DataTimeunpackPo(JSONObject jsobj) {
					String s="" ;
					System.err.println("------报警星期日====---------"+jsobj.getString("sun1"));
					System.err.println("------报警星期四====---------"+jsobj.getString("thur1"));
					Integer sunday=Integer.parseInt(jsobj.getString("sun1"));
					Integer monday=Integer.parseInt(jsobj.getString("mon1"));
					Integer tuesday=Integer.parseInt(jsobj.getString("tues1"));
					Integer wednesday=Integer.parseInt(jsobj.getString("wed1"));
					Integer thursday=Integer.parseInt(jsobj.getString("thur1"));
					System.out.println("---报警转换后的星期四----"+thursday);
					Integer friday=Integer.parseInt(jsobj.getString("fri1"));
					Integer saturday=Integer.parseInt(jsobj.getString("sat1"));
				Integer i =  (1<<sunday)+(4<<monday)
							         +(16<<tuesday)+(64<<wednesday)
							         +(256<<thursday)+(1024<<friday)
							         +(4096<<saturday);	        
				s=Integer.toHexString(i);//.substring(4, 8);
				System.out.println("报警数据库拼的s是====="+s);
				return s ;
				}
				
}
